﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace G.Controls
{
    public class VariableSizedGridView : GridView
    {
        public VariableSizedGridView()
        {
            this.DataTypes = new List<DataTemplate>();
        }

        public static readonly DependencyProperty DataTemplateDataTagProperty = DependencyProperty.RegisterAttached("DataTemplateDataTag", typeof(int), typeof(VariableSizedGridView), new PropertyMetadata(0));

        //private static void OnDataTemplateDataTypeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        //{
        //    //DataTemplate t = o as DataTemplate;
        //    //string type = DataTypedListBox.GetDataTemplateDataType(t);
        //    //if (!string.IsNullOrEmpty(type))
        //    //{
        //    //    DataTypedListBox.SetDataType(t, Type.GetType(type));
        //    //}
        //    //else
        //    //    DataTypedListBox.SetDataType(t, null);
        //}

        public static int GetDataTemplateDataTag(DataTemplate element)
        {
            return (int)element.GetValue(VariableSizedGridView.DataTemplateDataTagProperty);
        }

        public static void SetDataTemplateDataTag(DataTemplate element, int value)
        {
            element.SetValue(VariableSizedGridView.DataTemplateDataTagProperty, value);
        }

        public static readonly DependencyProperty DataTypesProperty = DependencyProperty.Register("DataTypes", typeof(List<DataTemplate>), typeof(VariableSizedGridView), null);

        public List<DataTemplate> DataTypes
        {
            get
            {
                return this.GetValue(VariableSizedGridView.DataTypesProperty) as List<DataTemplate>;
            }
            set
            {
                this.SetValue(VariableSizedGridView.DataTypesProperty, value);
            }
        }

        protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item)
        {
            G.Data.IGridItemSize gridItemSize = item as G.Data.IGridItemSize;
            ContentControl gridViewItem = element as ContentControl;
            if (gridItemSize != null)
            {
                //设置元素占用的列数和行数
                VariableSizedWrapGrid.SetColumnSpan(gridViewItem, gridItemSize.ColSpan);
                VariableSizedWrapGrid.SetRowSpan(gridViewItem, gridItemSize.RowSpan);
                //如果tag不为0，那么表示需要使用默认模板以外的模版
                if (gridItemSize.Tag == 0)
                {
                    base.PrepareContainerForItemOverride(element, item);
                }
                else
                {
                    //获取tag对应的模版
                    var a = this.DataTypes.FirstOrDefault(c => VariableSizedGridView.GetDataTemplateDataTag(c) == gridItemSize.Tag);
                    if (a == null)
                    {
                        base.PrepareContainerForItemOverride(element, item);
                    }
                    else
                    {
                        base.PrepareContainerForItemOverride(element, item);
                        //将模版赋值给gridViewItem
                        gridViewItem.ContentTemplate = a;
                    }
                }
            }
            else
                base.PrepareContainerForItemOverride(element, item);
        }
    }
}
